/**
 * @author denis.onishchenko@gmail.com
 */

package com.prapp.mediag;

import java.util.Date;
import java.util.List;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

final class Category {

    static final long ALL_CATEGORY_ID = -1L;
    static final long UNREAD_CATEGORY_ID = -2L;
    static final long FAVORITE_CATEGORY_ID = -3L;

    static boolean isSpecial(long id) {
        return id < 0;
    }

    static boolean isAll(long id) {
        return id == ALL_CATEGORY_ID;
    }

    static boolean isUnread(long id) {
        return id == UNREAD_CATEGORY_ID;
    }

    static boolean isFavorite(long id) {
        return id == FAVORITE_CATEGORY_ID;
    }

    private Long id;
    private String name;
    private String url;
    private Date lastUpdate;
    private String description;
    private String icon;

    Category() {
    }

    Category(Long id, String name) {
        this.id = id;
        this.name = name;
    }

    Category(String name, String url) {
        this.name = name;
        this.url = url;
    }

    public String toString() {
        return "Category{" +
                "id=" + id +
                '}';
    }

    Long getId() {
        return id;
    }

    void setId(Long id) {
        this.id = id;
    }

    String getName() {
        return name;
    }

    void setName(String name) {
        this.name = name;
    }

    String getUrl() {
        return url;
    }

    void setUrl(String url) {
        this.url = url;
    }

    Date getLastUpdate() {
        return lastUpdate;
    }

    void setLastUpdate(Date lastUpdate) {
        this.lastUpdate = lastUpdate;
    }

    String getDescription() {
        return description;
    }

    void setDescription(String description) {
        this.description = description;
    }

    String getIcon() {
        return icon;
    }

    void setIcon(String icon) {
        this.icon = icon;
    }

    boolean isSpecial() {
        return id != null && isSpecial(id);
    }

    boolean isAll() {
        return id != null && isAll(id);
    }

    boolean isUnread() {
        return id != null && isUnread(id);
    }

    boolean isFavorite() {
        return id != null && isFavorite(id);
    }

    static final class DBAdapter extends com.feeder.DBAdapter {

        static final String TABLE_NAME = "category";

        static final String COLUMN_ID = "_id";
        static final String COLUMN_NAME = "name";
        static final String COLUMN_URL = "url";
        static final String COLUMN_LAST_UPDATE = "last_update";
        static final String COLUMN_DESCRIPTION = "description";
        static final String COLUMN_ICON = "icon";

        static final String[] COLUMNS = {
                COLUMN_ID,
                COLUMN_NAME,
                COLUMN_URL,
                COLUMN_LAST_UPDATE,
                COLUMN_DESCRIPTION,
                COLUMN_ICON
        };

        void createTable(SQLiteDatabase db) {
            String sql = "CREATE TABLE " + TABLE_NAME + " (" +
                    COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT UNIQUE" +
                    ", " + COLUMN_NAME + " TEXT NOT NULL" +
                    ", " + COLUMN_URL + " TEXT NOT NULL UNIQUE" +
                    ", " + COLUMN_LAST_UPDATE + " INTEGER NOT NULL DEFAULT CURRENT_TIMESTAMP" +
                    ", " + COLUMN_DESCRIPTION + " TEXT" +
                    ", " + COLUMN_ICON + " TEXT" +
                    ")";

            db.execSQL(sql);
        }

        protected Category fromCursor(Cursor cursor) {
            Category category = new Category();

            category.setId(getLong(cursor, COLUMN_ID, true));
            category.setName(getString(cursor, COLUMN_NAME, true));
            category.setUrl(getString(cursor, COLUMN_URL, true));
            category.setLastUpdate(getDate(cursor, COLUMN_LAST_UPDATE, true));
            category.setDescription(getString(cursor, COLUMN_DESCRIPTION, false));
            category.setIcon(getString(cursor, COLUMN_ICON, false));

            return category;
        }

        protected ContentValues getContentValues(Object item) {
            Category category = (Category) item;

            ContentValues values = new ContentValues(5);

            if (category.getName() == null)
                throw new NullPointerException("Name is null");

            values.put(COLUMN_NAME, category.getName());

            if (category.getUrl() == null)
                throw new NullPointerException("Url is null");

            values.put(COLUMN_URL, category.getUrl());

            if (category.getLastUpdate() != null)
                values.put(COLUMN_LAST_UPDATE, date2Integer(category.getLastUpdate()));

            if (category.getDescription() != null)
                values.put(COLUMN_DESCRIPTION, category.getDescription());

            if (category.getIcon() != null)
                values.put(COLUMN_ICON, category.getIcon());

            return values;
        }

        void insert(Category category, SQLiteDatabase db) {
            long id = insert(category, TABLE_NAME, null, db);
            category.setId(id);
        }

        List queryList(SQLiteDatabase db) {
            return queryList(TABLE_NAME, COLUMNS, COLUMN_NAME, db);
        }

        Category queryByUrl(String url, SQLiteDatabase db) {
            String selection = COLUMN_URL + "=?";
            String[] selectionArgs = {url};

            return (Category) query(TABLE_NAME, COLUMNS, selection, selectionArgs, db);
        }

        void updateLastUpdate(long id, Date value, SQLiteDatabase db) {
            updateDate(id, value, TABLE_NAME, COLUMN_ID, COLUMN_LAST_UPDATE, db);
        }

        void delete(Category category, SQLiteDatabase db) {
            long id = category.getId();
            String whereClause = COLUMN_ID + "=?";
            String[] whereArgs = {String.valueOf(id)};
            db.delete(TABLE_NAME, whereClause, whereArgs);
        }
    }
}
